home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter5 / create.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  335 lines

  1.  
  2. #include <windows.h>  
  3. #include "create.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "ImageList_Create()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. #define NODRAG    0
  108. #define POSSIBLE  1
  109. #define DRAGING   2
  110.  
  111. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  112. {
  113. static POINT      pt, ptWnd, movePt;
  114. static RECT       rectangle;
  115. static HIMAGELIST hList     = NULL;
  116. static int        nDraging  = NODRAG;
  117. static int        nSel      = -1;
  118.  
  119.    switch( uMsg )
  120.    {
  121.       case WM_CREATE :
  122.               InitCommonControls();
  123.  
  124.               // Create the image list.
  125.               //.......................
  126.               hList = ImageList_Create( 32, 32, ILC_COLOR | ILC_MASK, 3, 1 );
  127.               if ( hList )
  128.               {
  129.                  HBITMAP hBmp, hMask;
  130.                  
  131.                  // Add masked image.
  132.                  //..................
  133.                  hBmp  = LoadBitmap( hInst, "BMP1" );
  134.                  hMask = LoadBitmap( hInst, "BMP1MSK" );
  135.                  ImageList_Add( hList, hBmp, hMask );
  136.                  DeleteObject( hBmp );
  137.                  DeleteObject( hMask );
  138.  
  139.                  // Add an image and build a mask.
  140.                  //...............................
  141.                  hBmp = LoadBitmap( hInst, "BMP2" );
  142.                  ImageList_AddMasked( hList, hBmp, RGB( 0, 255, 0 ) );
  143.                  DeleteObject( hBmp );
  144.  
  145.                  // Add an the arrow cursor.
  146.                  //.........................
  147.                  ImageList_AddIcon( hList, (HICON)LoadCursor( NULL, IDC_ARROW ) );
  148.  
  149.                  rectangle.left   = 10;
  150.                  rectangle.top    = 80;
  151.                  rectangle.right  = 250;
  152.                  rectangle.bottom = 160;
  153.               }
  154.               break;
  155.  
  156.       case WM_PAINT :
  157.               {
  158.                  PAINTSTRUCT ps;
  159.  
  160.                  // Paint the two images on the client area.
  161.                  //.........................................
  162.                  BeginPaint( hWnd, &ps );
  163.                  ImageList_Draw( hList, 0, ps.hdc, 10, 10, ILD_NORMAL | (nSel == 0 ? ILD_SELECTED : 0));
  164.                  ImageList_Draw( hList, 1, ps.hdc, 60, 10, ILD_NORMAL | (nSel == 1 ? ILD_SELECTED : 0));
  165.                  Rectangle( ps.hdc, rectangle.left, rectangle.top, rectangle.right, rectangle.bottom );
  166.                  TextOut( ps.hdc, rectangle.left+10, rectangle.top+10, "Drag cursor will not show here.", 31 );
  167.  
  168.                  // If the image is being dragged, get the drag image
  169.                  // and display it within the rectanlge on the client area.
  170.                  //........................................................
  171.                  if ( nDraging == DRAGING )
  172.                  {
  173.                     HIMAGELIST hDragList = ImageList_GetDragImage( NULL, NULL );
  174.                     int        cx,cy;
  175.  
  176.                     ImageList_GetIconSize( hDragList, &cx, &cy );
  177.                     ImageList_Draw( hDragList, 0, ps.hdc, 
  178.                                     rectangle.left+((rectangle.right-rectangle.left-cx)/2), 
  179.                                     rectangle.top+24, ILD_NORMAL ); 
  180.                  }
  181.                  EndPaint( hWnd, &ps );
  182.               }
  183.               break;
  184.  
  185.       case WM_LBUTTONDOWN :
  186.               {
  187.                  RECT  rect;
  188.  
  189.                  rect.top    = 10;
  190.                  rect.bottom = 42;
  191.                  rect.right  = 42;
  192.                  rect.left   = 10;
  193.  
  194.                  pt.x = LOWORD( lParam );
  195.                  pt.y = HIWORD( lParam );
  196.  
  197.                  nSel = -1;
  198.  
  199.                  // Check which image the user selected.
  200.                  //.....................................
  201.                  if ( PtInRect( &rect, pt ) )
  202.                     nSel = 0;
  203.  
  204.                  rect.right = 82;
  205.                  rect.left  = 50;
  206.                  if ( PtInRect( &rect, pt ) )
  207.                     nSel = 1;
  208.  
  209.                  InvalidateRect( hWnd, NULL, TRUE );
  210.  
  211.                  // Calculate the offsets from the main window to the client.
  212.                  //..........................................................
  213.                  if ( nSel > -1 )
  214.                  {
  215.                     nDraging = POSSIBLE;
  216.                     SetCapture( hWnd );
  217.  
  218.                     GetWindowRect( hWnd, &rect );
  219.                     ptWnd.x = rect.left;
  220.                     ptWnd.y = rect.top;
  221.  
  222.                     ScreenToClient( hWnd, &ptWnd );
  223.                     ptWnd.y += GetSystemMetrics( SM_CYSIZEFRAME )+3;
  224.                     ptWnd.x += GetSystemMetrics( SM_CXSIZEFRAME )+2;
  225.                  }
  226.               }
  227.               break;
  228.  
  229.       case WM_LBUTTONUP :
  230.               // If draging, Finish.
  231.               //....................
  232.               if ( nDraging == DRAGING ) 
  233.               {
  234.                  ImageList_DragLeave( hWnd );
  235.                  ImageList_EndDrag();
  236.                  ShowCursor( TRUE );
  237.               }
  238.  
  239.               nDraging = NODRAG;
  240.               ReleaseCapture();
  241.               InvalidateRect( hWnd, &rectangle, TRUE );
  242.               break;
  243.  
  244.       case WM_MOUSEMOVE :
  245.               if ( nDraging == POSSIBLE && 
  246.                    ( abs(LOWORD( lParam ) - pt.x) > 2 ||
  247.                      abs(HIWORD( lParam ) - pt.y) > 2 ) )
  248.               {
  249.                  nDraging = DRAGING;
  250.  
  251.                  // Hide the mouse cursor.
  252.                  //.......................
  253.                  ShowCursor( FALSE );
  254.  
  255.                  // Start Draging.
  256.                  //...............
  257.                  ImageList_BeginDrag( hList, nSel, pt.x-(15+nSel*50), pt.y-15 );
  258.  
  259.                  // Add the arrow to the drag cursor.
  260.                  //..................................
  261.                  ImageList_SetDragCursorImage( hList, 2, pt.x-(15+nSel*50), pt.y-15 );
  262.  
  263.                  // Show the drag cursor.
  264.                  //......................
  265.                  ImageList_DragEnter( hWnd, LOWORD( lParam )-ptWnd.x, HIWORD( lParam )-ptWnd.y );
  266.                  InvalidateRect( hWnd, &rectangle, TRUE );
  267.               }
  268.  
  269.               // If draging, move the drag cursor.
  270.               //..................................
  271.               if ( nDraging == DRAGING )
  272.               {
  273.                  movePt.x = LOWORD( lParam );
  274.                  movePt.y = HIWORD( lParam );
  275.                  if ( PtInRect( &rectangle, movePt ) )
  276.                     ImageList_DragShowNolock( FALSE );
  277.                  else
  278.                     ImageList_DragShowNolock( TRUE );
  279.  
  280.                  ImageList_DragMove( LOWORD( lParam )-ptWnd.x, HIWORD( lParam )-ptWnd.y );
  281.               }
  282.               break;
  283.  
  284.  
  285.       case WM_COMMAND :
  286.               switch( LOWORD( wParam ) )
  287.               {
  288.                  case IDM_ABOUT :
  289.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  290.                         break;
  291.  
  292.                  case IDM_EXIT :
  293.                         DestroyWindow( hWnd );
  294.                         break;
  295.               }
  296.               break;
  297.       
  298.       case WM_DESTROY :
  299.               if ( hList )
  300.                  ImageList_Destroy( hList );
  301.  
  302.               PostQuitMessage(0);
  303.               break;
  304.  
  305.       default :
  306.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  307.    }
  308.  
  309.    return( 0L );
  310. }
  311.  
  312.  
  313. LRESULT CALLBACK About( HWND hDlg,           
  314.                         UINT message,        
  315.                         WPARAM wParam,       
  316.                         LPARAM lParam)
  317. {
  318.    switch (message) 
  319.    {
  320.        case WM_INITDIALOG: 
  321.                return (TRUE);
  322.  
  323.        case WM_COMMAND:                              
  324.                if (   LOWORD(wParam) == IDOK         
  325.                    || LOWORD(wParam) == IDCANCEL)    
  326.                {
  327.                        EndDialog(hDlg, TRUE);        
  328.                        return (TRUE);
  329.                }
  330.                break;
  331.    }
  332.  
  333.    return (FALSE); 
  334. }
  335.